<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Geohashes 映射 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/geohash-mapping.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/geohash-mapping.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/geohash-mapping.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/geohash-mapping.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="geoloc.html">地理位置</a></span>
»
<span class="breadcrumb-link"><a href="geohashes.html">Geohashes</a></span>
»
<span class="breadcrumb-node">Geohashes 映射</span>
</div>
<div class="navheader">
<span class="prev">
<a href="geohashes.html">« Geohashes</a>
</span>
<span class="next">
<a href="geohash-cell-query.html">Geohash 单元查询 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="geohash-mapping"></a>Geohashes 映射<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/320_Geohashes/50_Geohash_mapping.asciidoc">edit</a>
</h2>
</div></div></div>
<p>首先，你需要决定使用什么样的精度。虽然你也可以使用 12 级的精度来索引所有的地理坐标点，但是你真的需要精确到数厘米吗？如果你把精度控制在一个实际一些的值，比如 <code class="literal">1km</code> ，那么你可以节省大量的索引空间：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">PUT /attractions
{
  "mappings": {
    "restaurant": {
      "properties": {
        "name": {
          "type": "string"
        },
        "location": {
          "type":               "geo_point",
          "geohash_prefix":     true, <a id="CO235-1"></a><i class="conum" data-value="1"></i>
          "geohash_precision":  "1km" <a id="CO235-2"></a><i class="conum" data-value="2"></i>
        }
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO235-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>将 <code class="literal">geohash_prefix</code> 设为 <code class="literal">true</code> 来告诉 Elasticsearch 使用指定精度来索引 geohash 的前缀。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO235-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>精度可以是一个具体的数字，代表的 geohash 的长度，也可以是一个距离。 <code class="literal">1km</code> 的精度对应的 geohash 的长度是 <code class="literal">7</code> 。</p>
</td>
</tr>
</table>
</div>
<p>通过如上设置， geohash 前缀中 1 到 7 的部分将被索引，所能提供的精度大约在 150 米。</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="geohashes.html">« Geohashes</a>
</span>
<span class="next">
<a href="geohash-cell-query.html">Geohash 单元查询 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>